using System;
using System.Collections.Generic;
using System.Linq;
using Headsight.BPMN2WF.BPMN.Schemas;

namespace Headsight.BPMN2WF.Testing.TestObjCtors
{
    /// <summary>
    /// <see cref="tDefinitions"/> <see cref="ITestObjCtor{T}"/>.
    /// </summary>
    public class tDefinitionsCtor : ITestObjCtor<tDefinitions>
    {
        private readonly IList<Func<tRootElement>> _tRootElems;
        private readonly IList<DiagramElement> _diagramElems;

        public tDefinitionsCtor()
        {
            _tRootElems = new List<Func<tRootElement>>();
            _diagramElems = new List<DiagramElement>();
        }

        public tDefinitions Create()
        {
            var result = new tDefinitions
            {
                Items = _tRootElems.Select(x => x()).ToArray(),
                BPMNDiagram = new[]
                {
                    new BPMNDiagram
                    {
                        BPMNPlane = new BPMNPlane
                        {
                            DiagramElements = _diagramElems.ToArray()
                        }
                    }
                }
            };

            return result;
        }

        public tDefinitionsCtor With(params tRootElement[] tRootElements)
        {
            foreach (var rootElement in tRootElements)
                _tRootElems.Add(() => rootElement);

            return this;
        }

        public tDefinitionsCtor WithDiagramElement(params BPMNShape[] bpmnShape)
        {
            foreach (var eachShape in bpmnShape)
                _diagramElems.Add(eachShape);

            return this;
        }

        public tDefinitionsCtor With<T>(ITestObjCtor<T> tRootElements) where T : tRootElement
        {
            _tRootElems.Add(tRootElements.Create);

            return this;
        }
    }
}